package com.twang.stock.utils.date;


import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.jayway.jsonpath.JsonPath;
import org.apache.commons.collections4.MapUtils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;


public class HolidayCopy {

    private static String toDady = "";

    /**
     * @return False 周末，TRUE-工作日
     * @Description: 入参格式为“yyyy-MM-dd”如“2021-3-27”
     */
    public static Boolean request() {
        String httpArg = getToday();
        //工作日对应结果为 0, 休息日对应结果为 1, 节假日对应的结果为 2
        String httpUrl = "http://v.juhe.cn/calendar/day?key=d1ad0fa736e2434219706c83af5d34a4";
        String result = "";
        StringBuffer sbf = new StringBuffer();
        httpUrl = httpUrl + "&date=" + httpArg;
        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            InputStream is = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = MapUtils.getString(jsonStringToMap(String.valueOf(sbf)), "result");
            String titless = JsonPath.read(result, "$.data.weekday");
            if ("星期六".equals(titless) || "星期日".equals(titless) || "星期七".equals(titless) || "星期天".equals(titless)) {
                return Boolean.TRUE;
            } else {
                return Boolean.FALSE;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Boolean.FALSE;
    }

    public static String getToday() {
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-dd");
        return sdf.format(now);
    }

    public static String getToday年月日分时秒(String date,String minutes) {
        return  date.substring(0,4)+"年"+date.substring(4,6)+"月"+date.substring(6,8)+"日 "
                +minutes.substring(0,2)+"时"+minutes.substring(2,4)+"分";
    }

    public static void main(String[] args) {

        //getToday年月日分时秒("20220302","1457");
        getHHmmss().substring(8,12);
    }

    public static String getRealToday() {
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(now);
    }

    public static String getHHmmss() {
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(now);
    }

    public static String getHHmmss(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmm");
        return sdf.format(date);
    }

    public static String getHHmmssByDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    public static String getYestoDay(){
        Calendar cal= Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        Date d=cal.getTime();
        SimpleDateFormat sp=new SimpleDateFormat("yyyyMMdd");
        //获取昨天日期
        return sp.format(d);
    }

    /**
     * 获取前天日期
     * @return
     */
    public static String getQianTian(){
        Calendar cal= Calendar.getInstance();
        cal.add(Calendar.DATE,-2);
        Date d=cal.getTime();
        SimpleDateFormat sp=new SimpleDateFormat("yyyyMMdd");
        //获取昨天日期
        return sp.format(d);
    }

    /**
     * @param
     * @return
     * @throws
     * @Description: JSONString转Map
     * @author liushiwei
     * @date 2018/9/27 15:42
     */
    private static Map<String, Object> jsonStringToMap(String json) {
        //String转成JSONObject形式
        JSONObject jsonArray = JSONObject.parseObject(json);
        Map<String, Object> param = JSONObject.parseObject(jsonArray.toJSONString(), new TypeReference<Map<String, Object>>() {
        });
        return param;
    }


}
